<?php
/**
 * @package Component Metromap for Joomla! 1.5
 * @version $Id: view.html.php 4 2010-08-17 13:58:48Z batyrkin $
 * @author Batyrkin Anton
 * @copyright (C) 2010- Batyrkin Anton
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 **/

defined( '_JEXEC' ) or die();
jimport( 'joomla.application.component.view' );

class MetromapViewMetromapLine extends JView
{

	function display($tpl = null)
	{
		global $mainframe;

		if($this->getLayout() == 'form') {
			$this->_displayForm($tpl);
			return;
		}

		parent::display($tpl);

	}

	function _displayForm($tpl) {
		global $mainframe, $option;

		$db		=& JFactory::getDBO();
		$uri 	=& JFactory::getURI();
		$user 	=& JFactory::getUser();
		$model	=& $this->getModel();
		$editor =& JFactory::getEditor();


		//Data from model
		$item	=& $this->get('Data');
		JHTML::stylesheet( 'metromap.css', 'administrator/components/com_metromap/assets/' );

		$lists 	= array();
		$isNew	= ((int)$item->id < 1);

		// fail if checked out not by 'me'
		if ($model->isCheckedOut( $user->get('id') )) {
			$msg = JText::sprintf( 'DESCBEINGEDITTED', JText::_( 'Metromap' ), $item->title );
			$mainframe->redirect( 'index.php?option='. $option, $msg );
		}

		// Set toolbar items for the page
		$text = $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
		JToolBarHelper::title(   JText::_( 'Line' ).': <small><small>[ ' . $text.' ]</small></small>' , 'station');
		JToolBarHelper::save();
		JToolBarHelper::apply();
		if ($isNew)  {
			JToolBarHelper::cancel();
		} else {
			// for existing items the button is renamed `close`
			JToolBarHelper::cancel( 'cancel', 'Close' );
		}
		JToolBarHelper::help( 'screen.metromap', true );

		// Edit or Create?
		if (!$isNew) {
			$model->checkout( $user->get('id') );
		} else {
			// initialise new record
			$item->order 			= 0;
		}

		// build the html select list for ordering
		$query = 'SELECT ordering AS value, linename AS text'
			. ' FROM #__metrolines'
			. ' ORDER BY ordering';
		$lists['ordering'] 			= JHTML::_('list.specificordering', $item, $item->id, $query, false );

		$this->assignRef('editor', $editor);
		$this->assignRef('lists', $lists);
		$this->assignRef('item', $item);
		$this->assignRef('tmpl', $tmpl);
		$this->assignRef('request_url',	htmlspecialchars($uri->toString()));

		parent::display($tpl);
	}
}
